package com.company.test.sync;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhangyan
 * @version $ Id: RLDemo2.java, v 0.1 2020/8/28 15:19 zhangyan Exp $
 */
public class RLDemo2 {
    private static final ReentrantLock lock = new ReentrantLock(true);

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
//                TimeUnit.DAYS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println("t1释放锁成功");
            }
        }, "第一个");
        t1.start();

        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

        try {
                TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Thread t2 = new Thread(() -> {
            lock.lock();
            System.out.println("t2");
            try {
                TimeUnit.DAYS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();

            }
        }, "第二个");
        t2.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb");

        Thread t3 = new Thread(() -> {
            lock.lock();
            System.out.println("t3");

            try {
//                TimeUnit.DAYS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "第三个");
        t3.start();

    }
}
